package com.sitanyun.merchant.guide.weiht;

import android.annotation.SuppressLint;
import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Context;
import android.text.TextUtils;

import java.util.List;

/**
 * Created by jzl
 * on 2022/2/11.
 */
public class GetTask {

    private List<ActivityManager.RunningTaskInfo> list;

    /**

     * 判断某个界面是否在前台

     * @param context

     *   上下文
     * @param className

     *    某个界面名称

     */

    public boolean isForeground(Context context, String className) {

        if (context == null || TextUtils.isEmpty(className)) {

            return false;

        }



        //ActivityManager的功能是与系统中所有运行着的Activity交互提供了接口

        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);

      list = am.getRunningTasks(1);

        if (list != null && list.size() > 0) {
            @SuppressLint({"NewApi", "LocalSuppress"})
            ComponentName cpn = list.get(0).topActivity;
            if (className.equals(cpn.getClassName())) {
                return true;

            }

        }

        return false;

    }
}
